package com.vend.main.home.presenter

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.launcher.ARouter
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.vend.common.base.BaseFragment
import com.vend.common.base.model.FragmentType
import com.vend.common.constant.RouterConstant
import com.vend.common.mvp.IPresenter
import com.vend.main.R
import com.vend.main.home.FragmentHome
import com.vend.main.home.HomeView
import com.vend.main.home.model.TabEntity
import java.util.ArrayList

/**
 * Created by glendal on 2022/01/25.
 * 首页的全部逻辑处理
 * 包括地图tab切换ViewPage的切换逻辑都在这个界面处理
 */
class HomePresenter(val self: Context, val v: HomeView, val manager: FragmentManager) : IPresenter {
    //点击信息的ententiy
    private val mTabEntities = ArrayList<CustomTabEntity>()

    //tab文本
    private val mTitles = arrayOf(
        self.getString(R.string.main_home_name),
        self.getString(R.string.main_shop_name),
        self.getString(R.string.main_my_name)
    )

    //未点击的ICon
    private val mIconUnselectIds = intArrayOf(
        R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher
    )

    //点击的Icon
    private val mIconSelectIds = intArrayOf(
        R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher
    )

    //fragment加载
    private val mFragments = ArrayList<Fragment>()

    //    //首页 这里去获取货物的首页数据
    private var mFragmentHome: Fragment =
        ARouter.getInstance().build(RouterConstant.GOOD_LIST_FRAGMENT).navigation() as Fragment

    //购物车  这里去获取购物车的数据
    private var mFragmentShop: Fragment =
        ARouter.getInstance().build(RouterConstant.SHOP_LIST_FRAGMENT).navigation() as Fragment

    //我的界面  这里去获取Self的数据
    private var mFragmentSelf: Fragment =
        ARouter.getInstance().build(RouterConstant.USER_HOME).navigation() as Fragment


    private fun initTabData() {
        for (i in mTitles.indices) {
            mTabEntities.add(TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]))
        }
        mFragments.add(mFragmentHome)
        mFragments.add(mFragmentShop)
        mFragments.add(mFragmentSelf)
        tabLayoutInit()
    }

    /**
     * 初始化底部TabLayout
     */
    private fun tabLayoutInit() {
        v.getViewPage().adapter = MyPagerAdapter(manager, mFragments, mTitles = mTitles)
        v.getTabLayout().setTabData(mTabEntities)
        v.getTabLayout().setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                v.getViewPage()?.currentItem = position
            }

            override fun onTabReselect(position: Int) {
            }
        })
        v.getViewPage().addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                v.getTabLayout().currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        v.getViewPage().currentItem = 0
    }

    /**
     * fragment FragmentPageAdapter适配器
     */
    private class MyPagerAdapter(
        val fm: FragmentManager?,
        val mFragments: ArrayList<Fragment>,
        val mTitles: Array<String>
    ) :
        FragmentPagerAdapter(fm!!) {
        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitles.get(position)
        }

        override fun getItem(position: Int): Fragment {
            return mFragments.get(position)
        }
    }

    /**
     *防止有事加载Fragment存在错乱的处理
     *
     */
    public fun attachFragment(fragment: Fragment) {
        if (fragment is BaseFragment<*>) {
            val b: Int = (fragment as BaseFragment<*>).getFragmentType()
            when (b) {
                FragmentType.Fragment_Home.fType -> {
                    mFragmentHome = fragment
                }
                FragmentType.Fragment_Shop.fType -> {
                    mFragmentShop = fragment
                }
                FragmentType.Fragment_Self.fType -> {
                    mFragmentSelf = fragment
                }
            }
        }
    }


    override fun start() {
        initTabData()
    }

    override fun stop() {

    }

}